package com.itheima02.dao;

import com.itheima02.domain.Student;
import com.itheima02.utils.JDBCUtils;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@SuppressWarnings("all")
public class StudentDaoImpl implements StudentDao {
    ArrayList<Student> list = new ArrayList<>();
    Student stu = null;
    ResultSet res = null;
    PreparedStatement ps = null;
    Connection con = JDBCUtils.getConnection();

    /*
        查询所有学生信息
     */
    @Override
    public ArrayList<Student> findAll() {

        try {
            String sql = "select * from student";
            ps = con.prepareStatement(sql);
            res = ps.executeQuery();
            while (res.next()) {
                Integer sid = res.getInt("sid");
                String name = res.getString("name");
                Integer age = res.getInt("age");
                Date birthday = res.getDate("birthday");
                Student stu = new Student(sid, name, age, birthday);
                list.add(stu);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(res, ps, con);
        }
        return list;
    }

    /*
        条件查询，根据id查询学生信息
     */
    @Override
    public Student findById(Integer id) {
        try {
            String sql = "select * from student where sid=?";
            ps = con.prepareStatement(sql);
            ps.setObject(1, id);
            res = ps.executeQuery();
            if (res.next()) {
                Integer sid = res.getInt("sid");
                String name = res.getString("name");
                Integer age = res.getInt("age");
                Date birthday = res.getDate("birthday");
                stu = new Student(sid, name, age, birthday);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(res, ps, con);
        }
        return stu;
    }

    /*
        添加学生信息
     */
    @Override
    public int insert(Student stu) {
        int res = 0;
        try {
            String sql = "insert into student value (?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, stu.getSid());
            ps.setObject(2, stu.getName());
            ps.setObject(3, stu.getAge());
            Date birth = stu.getBirthday();
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(birth);
            ps.setObject(4, birthday);
            res = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,ps, con);
        }
        return res;
    }

    /*
        修改学生信息
     */
    @Override
    public int update(Student stu,Integer id) {
        int res = 0;
        try {
            String sql = "UPDATE student SET sid=?,name=?,age=?,birthday=? WHERE sid=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, id);
            ps.setObject(2, stu.getName());
            ps.setObject(3, stu.getAge());
            Date date = stu.getBirthday();
            String birthday = new SimpleDateFormat("yyyy-MM-dd").format(date);
            ps.setObject(4, birthday);
            ps.setObject(5, stu.getSid());
            res = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,ps, con);
        }
        return res;
    }

    /*
        删除学生信息
     */
    @Override
    public int delete(Integer id) {
        int res = 0;
        try {
            String sql = "delete from student where sid=?";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1,id);
            res = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(null,ps, con);
        }
        return res;
    }
}
